﻿using AutoMapper;
using BasicData.API.Write.Application.Commands;
using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infrastructure.interfaces;
using MediatR;

namespace BasicData.API.Write.Application.CommandHandlers
{
    public class CreateGoodsCommandHand : IRequestHandler<CreateGoodsCommand, APIResult<int>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<GoodsModel> baseRepository;

        public CreateGoodsCommandHand(IMapper mapper,IBaseRepository<GoodsModel> baseRepository)
        {
            this.mapper = mapper;
            this.baseRepository = baseRepository;
        }

        public async Task<APIResult<int>> Handle(CreateGoodsCommand request, CancellationToken cancellationToken)
        {
            var map=mapper.Map<GoodsModel>(request);
            var r=await baseRepository.AddAsync(map);
            return APIResult<int>.Success("添加成功", r);
        }
    }
}
